// 此文件模拟后端服务器
import express from "express";
import bodyParser from "body-parser";
const app = express();

app.use(bodyParser.json());

const token = "jwt abcabc";
let userInfo = {};

app.post("/login", (req, res) => {
  let data = {};
  if (req.body) {
    userInfo = { ...req.body, token };
    data = {
      code: 0,
      result: userInfo,
      message: "success"
    };
  } else {
    data = {
      code: 404,
      result: null,
      message: "fail"
    };
  }
  res.send(data);
});

app.get("/user", (req, res) => {
  const { token: key } = req.query;
  let data = {};
  if (key === token) {
    data = {
      code: 0,
      result: userInfo,
      message: "success"
    };
  } else {
    data = {
      code: 404,
      result: null,
      message: "fail"
    };
  }
  res.send(data);
});

app.get("/", (req, res) => {
  res.send("hello world!");
});

// 监听端口
app.listen(3000, () => {
  console.log("server on http://localhost:3000");
});

//! tips 这个文件定义为 mjs 说明使用 es 模块导入导出，
//! 本来是可以在 package.json 中设置 type 类型的, 但是 eslint 检测不通过，所有将文件设置为 .mjs 支持 es 导入导出才能执行
